<template>
  <div class="f-menu" :style="{ width: $store.state.asideWidth }">
    <el-menu :default-active="defaultActive" unique-opened :collapse="isCollapse" default-active="2" class="border-0"
             @select="handleSelect" :collapse-transition="false">
      <template v-for="(item, index) in asideMenus" :key="index">
        <el-sub-menu v-if="item.child && item.child.length > 0" :index="item.name">
          <template #title>
            <el-icon :size="20">
              <component :is="item.icon"></component>
            </el-icon>
            <span>{{ item.name }}</span>
          </template>
          <el-menu-item v-for="(item2, index2) in item.child" :key="index2" :index="item2.frontpath">
            <el-icon :size="20">
              <component :is="item2.icon"></component>
            </el-icon>
            <span>{{ item2.name }}</span>
          </el-menu-item>
        </el-sub-menu>

        <el-menu-item v-else :index="item.frontpath">
          <el-icon :size="20">
            <component :is="item.icon"></component>
          </el-icon>
          <span>{{ item.name }}</span>
        </el-menu-item>
      </template>
    </el-menu>
  </div>
</template>
<script setup>
import {computed, ref} from 'vue';
import {useRouter, useRoute, onBeforeRouteUpdate} from 'vue-router';
import {useStore} from 'vuex';

const router = useRouter()
const store = useStore()
const route = useRoute()

// 默认选中
const defaultActive = ref(route.path)

// 监听路由变化
onBeforeRouteUpdate((to, from) => {
  defaultActive.value = to.path
})

// 是否折叠
const isCollapse = computed(() => !(store.state.asideWidth == '250px'))

// const asideMenus = computed(() => store.state.menus)
const asideMenus = [
  {
    "name": "后台面板",
    "icon": "help",
    "child": [{
      "name": "主控台",
      "icon": "home-filled",
      "frontpath": "/",
    }]
  },
  {
    "name": "设备管理",
    "icon": "box",
    "child": [
      {
        "name": "传感器设备管理",
        "icon": "cpu",
        "frontpath": "/device/management",
      },
      {
        "name": "传感器数据查看",
        "icon": "search",
        "frontpath": "/device/view",
      },
      {
        "name": "摄像头设备管理",
        "icon": "camera",
        "frontpath": "/device/camera",
      }]
  },
  {
    "name": "病虫害预警",
    "icon": "WarningFilled",
    "child": [
      {
        "name": "数据可视化",
        "icon": "eleme",
        "frontpath": "/disease/data-visualization",
      },
      {
        "name": "历史数据",
        "icon": "Histogram",
        "frontpath": "/disease/historical-data",
      },
      {
        "name": "当前数据",
        "icon": "Grid",
        "frontpath": "/disease/real-time-data",
      },
      {
        "name": "病害预警",
        "icon": "warning",
        "frontpath": "/disease/disease-warning",
      },
      {
        "name": "防治信息",
        "icon": "QuestionFilled",
        "frontpath": "/disease/prevention-control-info",
      },
      {
        "name": "数据导入",
        "icon": "upload",
        "frontpath": "/disease/data-import",
      },
      {
        "name": "报表导出",
        "icon": "download",
        "frontpath": "/disease/report-export",
      },
      {
        "name": "定时任务",
        "icon": "clock",
        "frontpath": "/disease/scheduled-tasks",
      },
      {
        "name": "消息通知",
        "icon": "bell",
        "frontpath": "/disease/notification-system",
      }]
  },]
const handleSelect = (e) => {
  if (e == "video") {
    return window.open("https://study.163.com/course/courseMain.htm?courseId=1212775807&share=2&shareId=480000001892585")
  }
  router.push(e)
}
</script>
<style>
.f-menu {
  transition: all 0.2s;
  top: 64px;
  bottom: 0;
  left: 0;
  overflow-y: auto;
  overflow-x: hidden;
  @apply shadow-md fixed bg-light-50;
}

.f-menu::-webkit-scrollbar {
  width: 0px;
}
</style>
